package com.sysolve.betterCity.service;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import com.sysolve.betterCity.common.Help;
import com.sysolve.betterCity.model.Activity;
import com.sysolve.betterCity.model.HotArea;
import com.sysolve.betterCity.model.Place;

public class CalcHotArea implements Runnable {
	
	public int radius;
	public boolean isRunning = false;
	public Date calcTime;
	public int heightLevel;
	public List<HotArea> hotAreas = null;
	public int validPointCount = 0;
	
	//public String result = null;
	
	public CalcHotArea(int radius, int heightLevel) {
		super();
		this.radius = radius;
		this.heightLevel = heightLevel;
	}
	public synchronized boolean startCalc() {
		if (isRunning) 
			return false;
		else {
			isRunning = true;
			this.hotAreas = null;
			calcTime = new Date();
			new Thread(this).run();
			return true;
		}
	}
	public void run() {
		List<Object[]> list = BetterCityService.getInstance().listAllVaildPlace();
		this.hotAreas = null;
		List<HotArea> hotAreas = new LinkedList<HotArea>();
				
		int count = 0;
		for (Object[] obj: list) {
			//int id = (Integer)(obj[0]);
			int lat = (Integer)(obj[1]);
			int lng = (Integer)(obj[2]);
			int h = ((Long)(obj[3])).intValue();
			count+=h;
			Place p = new Place(lat,lng);
			HotArea area = null; 
			for (HotArea a:hotAreas) {
				if (a.isPlaceNearBy(p, radius)) {
					if (area==null) {
						a.addPlace(p, radius, h);
						area = a;
					} else {
						a.addPlace(p, radius, 0);
						area.addHotArea(a);
						a.setHeight(0);
						list.remove(a);
					}
				}
			}
			if (area==null) 
				hotAreas.add(new HotArea(p, radius, h));
		}
		
		validPointCount = count;
		
		System.out.println("有效数据点："+count);
		System.out.println("扩散半径："+radius);
		
		System.out.println("热点区域："+hotAreas.size());
		int totalHeight=0;
		int maxHeight=0;
		for (HotArea a:hotAreas) {
			totalHeight+=a.getHeight();
			if (a.getHeight()>maxHeight) maxHeight=a.getHeight();
		}
		System.out.println(totalHeight+"\t"+maxHeight);
		
		StringBuffer sb = new StringBuffer();
		sb.append("<p><strong>根据大家的活动记录，计算的繁华圈如下：</strong><br>");
		sb.append("计算时间："+Help.DATETIME_FORMAT.format(calcTime));sb.append("<br>");
		sb.append("有效数据点："+validPointCount);sb.append("<br>");
		sb.append("扩散半径："+radius);sb.append("<br>");
		sb.append("最小高度："+heightLevel);sb.append("<br></p>");
		
		for (HotArea a:hotAreas) {
			if (a.getHeight()>=heightLevel) {
				sb.append("<li>");
				sb.append("<img src=\""+ Help.WEBAPP_URL +"/images/activity.gif\" />");
				sb.append("<a href=\"javascript:void(0);\" onclick=\"maps.addOverLay(drawRect("+a.getLatMin()+","+a.getLngMin()+","+a.getLatMax()+","+a.getLngMax()+",'#ff0000'));centerAndZoom("+a.getCenterLat()+","+a.getCenterLng()+",5);\">");
				
				String st = " ";
				while (" ".equals(st)) 
					st = Help.selectPlace(a.getCenterLng(), a.getCenterLat());
				
				sb.append(st);
				//out.print(a.getLatMin());System.out.print("\t");
				//out.print(a.getLatMax());System.out.print("\t");
				//out.print(a.getLngMin());System.out.print("\t");
				//out.print(a.getLngMax());System.out.print("\t");
				sb.append("</a>");
				sb.append(" 高度："+a.getHeight());System.out.println();
				sb.append("</li>");
				
				System.out.print(st);
				System.out.print("\t");
				System.out.print(a.getLatMin());System.out.print("\t");
				System.out.print(a.getLatMax());System.out.print("\t");
				System.out.print(a.getLngMin());System.out.print("\t");
				System.out.print(a.getLngMax());System.out.print("\t");
				System.out.print(a.getHeight());System.out.println();
			}
		}
		this.calcResult = sb.toString();
		this.hotAreas = hotAreas;
		
		isRunning = false;
	}
	
	public String calcResult;
	
	public static void main(String[] args) {
		new CalcHotArea(300,2).startCalc();
	}
}
